package com.tj.jinzao.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseActivity;
import com.tj.jinzao.base.ConstantApp;
import com.tj.jinzao.callback.IUploadView;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.model.Article;
import com.tj.jinzao.model.Target;
import com.tj.jinzao.presenter.PublicArticlePresenter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 发布日记3
 * Created by wjy on 2019/1/12.
 */
public class PublicTopic3Activity extends BaseActivity<PublicArticlePresenter> implements IUploadView<String> {

    @BindView(R.id.ll_content_layout)
    LinearLayout mContentLayout;
    @BindView(R.id.tv_title)
    TextView mTitleView;
    @BindView(R.id.tag)
    TextView tag_view;

    private boolean mChooseVideo;
    private List<String> mImageList;
    private String mContent;

    private Article mArticle;

    @Override
    protected PublicArticlePresenter createPresenter() {
        return new PublicArticlePresenter(this);
    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_public_topic_3;
    }

    @Override
    protected int getLeftButtonResId() {
        return R.mipmap.ic_back;
    }

    @Override
    protected CharSequence getRightButtonText() {
        return "发布";
    }

    @Override
    protected void clickRightButton() {
        if (checkUploadResource()) {
            if(ConstantApp.isFastDoubleClick(1500)){
                return;
            }

            mPresenter.publicTopic(mArticle.getTitle(), mContent, tag == null
                    ? "" : tag, mArticle.getImgurls(), mArticle.isVideo(), mArticle.getVideoUrl());
        }
    }
    private String tag;

    /**
     * 循环检测发送资源是否已经上传
     */
    public boolean checkUploadResource() {
        if (mImageList != null && mImageList.size() > 0) {
            StringBuilder imageUrls = null;
            for (String path : mImageList) {
                if (TextUtils.equals(path, "0")) {
                    continue;
                }
                if (path.startsWith(Constant.SD_ROOT)) {
                    mPresenter.uploadFile(path);
                    return false;
                } else {
                    if (imageUrls == null) imageUrls = new StringBuilder();
                    else imageUrls.append(",");
                    imageUrls.append(path);
                }
            }
            String imageStr = imageUrls == null ? "" : imageUrls.toString();
            mArticle.setImgurls(imageStr);
        }
        if (mChooseVideo) {
            if (mArticle.getVideoUrl().startsWith(Constant.SD_ROOT)) {
                mPresenter.uploadFile(mArticle.getVideoUrl());
                return false;
            }
        }
        return true;
    }

    @Override
    protected void setupView() {
        mImageList = new ArrayList();
        try {
            mArticle = (Article) getIntent().getSerializableExtra("article");
            String images = mArticle.getImgurls();
            if (!TextUtils.isEmpty(images)) {
                String imageArray[] = images.split(",");
                mImageList = Arrays.asList(imageArray);
            }
            mContent = mArticle.getContent();
            mChooseVideo = mArticle.isVideo();
            setContent();
        } catch (Exception e) {
        }
    }


    @OnClick(R.id.rl_label_layout)
    public void chooseLabel() {
        startActivityForResult(new Intent(this, LabelActivity.class), 100);
    }

    private void setContent() {
        mTitleView.setText("标题：" + mArticle.getTitle());
        mPresenter.setShowContentView(getLayoutInflater(), mContent, mImageList
                , mChooseVideo, mContentLayout, null);
    }
    private ArrayList<String> list=new ArrayList<>();


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 结果码不等于取消时候
        if (resultCode == 100) {
            try {
                tag=null;
                list =  data.getStringArrayListExtra("label");
                for (int i=0;i<list.size();i++){
                    if(i==0){
                        tag=list.get(i);
                    }else {
                        tag=tag+","+list.get(i);
                    }
                }
                if(tag!=null){
                    tag_view.setText(tag);
                }
            } catch (Exception e) {
            }
        }
    }

    @Override
    public void onUploadSuccess(String path, String url) {
        if (mImageList != null) {
            for (int i = 0; i < mImageList.size(); i++) {
                if (TextUtils.equals(mImageList.get(i), path)) {
                    mImageList.set(i, url);
                    break;
                }
            }
        }
        if (TextUtils.equals(mArticle.getVideoUrl(), path)) {
            mArticle.setVideoUrl(url);
        }
        clickRightButton();
    }

    @Override
    public void onCaseSuccess(List<Target> list) {

    }

    @Override
    public void onCaseFailed(String msg) {

    }

    @Override
    public Activity getContext() {
        return this;
    }

    @Override
    public void onRequestSuccess(String s) {

    }

    @Override
    public void onRequestNoData(String message) {

    }

    @Override
    public void onRequestError(String error_message) {

    }

}
